<template>
    <el-button type="primary" plain icon="el-icon-search" @click="search">查询</el-button>
    <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">添加</el-button>

    <div style="margin-top:15px">
        <el-table ref="testTable" :data="tableData" style="width:100%" border>
            <el-table-column prop="jobName" label="任务名称" sortable show-overflow-tooltip/>
            <el-table-column prop="jobGroup" label="任务所在组" sortable/>
            <el-table-column prop="jobClassName" label="任务类名" sortable/>
            <el-table-column prop="triggerName" label="触发器名称" sortable/>
            <el-table-column prop="triggerGroup" label="触发器所在组" sortable/>
            <el-table-column prop="cronExpression" label="表达式" sortable/>
            <el-table-column prop="triggerState" label="任务状态" :formatter="statusFormat"/>
            <el-table-column prop="timeZoneId" label="时区" sortable/>
            <el-table-column label="操作" width="300">
                <template v-slot="scope">
                    <el-button size="small" type="warning"
                               @click="handlePause(scope.$index, scope.row)">暂停
                    </el-button>
                    <el-button size="small" type="info"
                               @click="handleResume(scope.$index, scope.row)">恢复
                    </el-button>
                    <el-button size="small" type="danger"
                               @click="handleDelete(scope.$index, scope.row)">删除
                    </el-button>
                    <el-button size="small" type="success"
                               @click="handleUpdate(scope.$index, scope.row)">修改
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <div align="center">
            <el-pagination @size-change="handleSizeChange"
                           @current-change="handleCurrentChange"
                           :current-page="queryParams.pageNum"
                           :page-sizes="[10, 20, 30, 40]"
                           :page-size="queryParams.pageSize"
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="totalCount">
            </el-pagination>
        </div>
    </div>

    <el-dialog title="添加任务" v-model="dialogFormVisible" :width="600">
        <el-form ref="form" :model="form" :rules="addRules" label-width="120px">
            <el-form-item label="任务名称">
                <el-input v-model="form.jobName"></el-input>
            </el-form-item>
            <el-form-item label="任务分组">
                <el-input v-model="form.jobGroup"></el-input>
            </el-form-item>
            <el-form-item label="表达式">
                <el-input v-model="form.cronExpression"></el-input>
            </el-form-item>
            <el-form-item label="任务类名">
                <el-input v-model="form.jobClassName"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="add">确 定</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改任务" v-model="updateFormVisible">
        <el-form :model="updateForm">
            <el-form-item label="表达式" label-width="120px" style="width:35%">
                <el-input v-model="updateForm.cronExpression" auto-complete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="updateFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="update">确 定</el-button>
        </div>
    </el-dialog>

    <footer align="center">
        <p>&copy; Quartz 任务管理</p>
    </footer>
</template>

<script>
    import {listJob, addJob, updateJob, deleteJob, pauseJob, resumeJob} from '/src/api/job'

    export default {
        name: "index",
        data() {
            return {
                statusOptions: [],
                //表格当前页数据
                tableData: [],
                totalCount: 0,
                //请求参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                },
                //添加对话框默认可见性
                dialogFormVisible: false,
                addRules: {
                    jobName: [
                        {required: true, message: "任务名称不能为空", trigger: "blur"}
                    ],
                    jobGroup: [
                        {required: true, message: "任务分组不能为空", trigger: "blur"}
                    ],
                    cronExpression: [
                        {required: true, message: "cron表达式不能为空", trigger: "blur"}
                    ],
                    jobClassName: [
                        {required: true, message: "任务类名不能为空", trigger: "blur"}
                    ],
                },
                //修改对话框默认可见性
                updateFormVisible: false,
                //提交的表单
                form: {
                    jobName: '',
                    jobGroup: '',
                    cronExpression: '',
                },
                updateForm: {
                    jobName: '',
                    jobGroup: '',
                    cronExpression: '',
                },
            }
        },
        created() {
            this.getDicts("quartz_job_status").then(response => {
                this.statusOptions = response.data;
            });
            this.loadData()
        },
        methods: {
            //从服务器读取数据
            loadData() {
                listJob(this.queryParams).then(res => {
                    console.log(res)
                    this.tableData = res.rows;
                    this.totalCount = res.total;
                })
            },

            //单行删除
            handleDelete(index, row) {
                deleteJob({
                    "jobName": row.jobName,
                    "jobGroupName": row.jobGroup
                }).then(res => {
                    this.loadData();
                });
            },

            //暂停任务
            handlePause(index, row) {
                pauseJob({
                    "jobName": row.jobName,
                    "jobGroupName": row.jobGroup
                }).then(res => {
                    this.loadData();
                });
            },

            //恢复任务
            handleResume(index, row) {
                resumeJob({
                    "jobName": row.jobName,
                    "jobGroupName": row.jobGroup
                }).then(res => {
                    this.loadData();
                });
            },

            //搜索
            search() {
                this.loadData();
            },

            //弹出对话框
            handleAdd() {
                this.dialogFormVisible = true;
            },

            //添加
            add() {
                addJob(this.form).then(res => {
                    this.loadData();
                    this.dialogFormVisible = false;
                }).catch(function () {
                    this.dialogFormVisible = false;
                });
            },

            //更新
            handleUpdate(index, row) {
                this.updateFormVisible = true;
                this.updateForm.jobName = row.jobName;
                this.updateForm.jobGroup = row.jobGroup;
            },

            //更新任务
            update() {
                updateJob(this.updateForm).then(res => {
                    this.updateFormVisible = false;
                    this.loadData();
                }).catch(function () {
                    this.updateFormVisible = false;
                });
                ;
            },

            //每页显示数据量变更
            handleSizeChange(val) {
                this.queryParams.pageSize = val;
                this.loadData();
            },
            //页码变更
            handleCurrentChange(val) {
                this.queryParams.pageNum = val;
                this.loadData();
            },
            // 菜单状态字典翻译
            statusFormat(row, column) {
                return this.selectDictLabel(this.statusOptions, row.triggerState);
            },
        }
    }
</script>

<style scoped>

</style>
